package com.city.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.city.eduservice.entity.EduVideo;
import com.city.eduservice.feignClient.VodServiceFeignClient;
import com.city.eduservice.mapper.EduVideoMapper;
import com.city.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author city
 * @since 2020-07-01
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodServiceFeignClient vodServiceFeignClient;
    /**
     * TODO 删除阿里云视频
     * @param courseId
     */
    @Override
    public void removeVideoByCourseId(String courseId) {
        //删除课程id为courseId得多个小节得阿里云存储视频
        QueryWrapper<EduVideo> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        queryWrapper1.select("video_source_id");//指定只查某个字段，而不是查怎么对象出来，浪费资源
        List<EduVideo> eduVideos = baseMapper.selectList(queryWrapper1);

        List<String> videoIds=new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                videoIds.add(videoSourceId);
            }
        }
        vodServiceFeignClient.deleteBatch(videoIds);

        //删除课程id为courseId得小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
